package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/17 14:15
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeyidSecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    DtsStorageMapper storageMapper;

    @Override
    public DtsStorage uploadFile(String name,Integer size,String type,String objectName,byte[] bytes) throws Exception {

//        上传文件到oss
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeyidSecret, bucketName, name, bytes);
        DtsStorage storage = new DtsStorage();
        storage.setAddTime(new Date());
        storage.setDeleted(false);
        storage.setKey(name); //文件的唯一名称
        storage.setName(objectName);//文件的原始名称
        storage.setSize(size);
        storage.setType(type);
        storage.setUpdateTime(new Date());
        storage.setUrl(url); //图片的url路径

//        存储数据到数据库
        storageMapper.insert(storage);
        return storage;
    }

    @Override
    public Map<String, Object> selectByPage(Integer page, Integer limit, String sort, String order,String key,String name) {
        IPage<DtsStorage> iPage = new Page<>(page,limit);
        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            wrapper.like("`key`",key);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("`name`",name);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        iPage = storageMapper.selectPage(iPage,wrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public void delete(DtsStorage storage) {
//        先删除数据库
        storageMapper.deleteById(storage);
//        后删除oss上的资源
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeyidSecret,bucketName,storage.getKey());
    }
}
